
#include <iostream>
#include <string.h>
#include <assert.h>

using namespace std;

namespace my_string
{
    class string 
    {
    public:
        typedef char* iterator;
        typedef const char* const_iterator;
        
        iterator begin()
        {
            return _str;
        }

        const_iterator begin() const
        {
            return _str;
        }

        iterator end()
        {
            return _str + _size;
        }

        const_iterator end() const 
        {
            return _str + _size;
        }

        string(const char* str = "")
        {
            _size = strlen(str);
            _capacity = _size;
            _str = new char[_capacity + 1];
    
            strcpy(_str, str);
        }

        string(const string& s)
            :_size(s._size)
            , _capacity(s._capacity)
        {
            _str = new char[_capacity + 1];
            strcpy(_str, s._str);
        }

        string& operator=(const string& s)
        {
            char* tmp = new char[s._capacity + 1];
            strcpy(tmp, s._str);

            delete[] _str;
            _str = tmp;
            _size = s._size;
            _capacity = s._capacity;

            return *this;
        }

        ~string()
        {
            delete[] _str;
            _str = nullptr;
            _size = _capacity = 0;
        }

        const char* c_str() const 
        {
            return _str;
        }

        size_t size() const 
        {
            return _size;
        }

        bool empty() const 
        {
            return _size == 0;
        }

        char& operator[](size_t pos)
        {
            assert(pos < _size);

            return _str[pos];
        }

        const char& operator[](size_t pos) const 
        {
            assert(pos < _size);

            return _str[pos];
        }
    
    private:
        char* _str;
        size_t _size;
        size_t _capacity;
    };


    void test()
    {
        string s1("hello linux !!!");

        for (size_t i = 0; i < s1.size(); i ++ )
        {
            cout << s1[i] << ' ';
        }
        cout << endl;

        string s2(s1);
        string::iterator it = s2.begin();
        while (it != s2.end())
        {
            cout << *it << ' ';
            ++ it;
        }
        cout << endl;

        string s3("hello world !!!");
        for (size_t i = 0; i < s3.size(); i ++ )
        {
            cout << s3[i] << ' ';
        }
        cout << endl;

        s3 = s2;

        string::iterator itt = s3.begin();
        while (itt != s3.end())
        {
            cout << *itt << ' ';
            itt ++ ;
        }
        cout << endl;

        for (size_t i = 0; i < s3.size(); i ++ )
        {
            s3[i] ++ ;
        }

        for (size_t i = 0; i < s3.size(); i ++ )
        {
            cout << s3[i] << ' ';
        }
        cout << endl;
    }
}
